#include "libactivation.h"


Libactivation::Libactivation()
{
    DBusGConnection *connection = NULL;
    GError *error = NULL;
    if((connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL) {
        qDebug() << "dbus initialization failed！";
    }
//    DBusGProxy *proxy = NULL;
    if((proxy = dbus_g_proxy_new_for_name(connection, "org.freedesktop.activation", "/org/freedesktop/activation", "org.freedesktop.activation.interface")) == NULL) {
        qDebug() << "dbus connection failed！";
    }
}


//获取硬盘序列号
QString Libactivation::get_harddisk_id()
{
    int ret = 0;
    GError *error = NULL;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_harddisk_id", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
//        printf("get_encrypted_hardware_info->%s\n", request_name_result);
//        printf("get_encrypted_hardware_info->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//获取UUID
QString Libactivation::get_system_uuid()
{
    int ret = 0;
    GError *error = NULL;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_system_uuid", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
//        printf("get_encrypted_hardware_info->%s\n", request_name_result);
//        printf("get_encrypted_hardware_info->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//获取网卡Mac
QString Libactivation::get_interface_mac()
{
    int ret = 0;
    GError *error = NULL;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_interface_mac", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
//        printf("get_encrypted_hardware_info->%s\n", request_name_result);
//        printf("get_encrypted_hardware_info->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//获取注册码
QString Libactivation::new_register_number(gchar *serial)
{
    int ret = 0;
    gchar new_serial[20]="0";
    GError *error = NULL;
    gchar *request_name_result = NULL;
    guint request_name_int =0;

    if(strlen(serial) == 7)
        strcat(new_serial, serial);
    else
        strcpy(new_serial, serial);
//    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "new_register_number", &error,
                    G_TYPE_STRING, new_serial,
                    G_TYPE_INVALID,
//                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
//        printf("new_register_number->%s\n", request_name_result);
//        printf("new_register_number_i->%d\n",request_name_int);

        error = NULL;
    }
    return request_name_result;
}

//获取激活状态
int Libactivation::status_code()
{
    int ret = 0;
    GError *error = NULL;
    gint request_name_result = 1;
    guint request_name_int =0;
//    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "status", &error,
                    G_TYPE_INVALID,
//                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
//        printf("status_value->%d\n", request_name_result);
//        printf("status_value_i->%d\n",request_name_int);
        error = NULL;
    }
    if(request_name_result == 0 || trial_status()==1)
        request_name_result = 1;

    return request_name_result;
}

int Libactivation::trial_status()
{
    int ret = 0;
    GError *error = NULL;
    gint request_name_result = 1;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "trial_status", &error,
                    G_TYPE_INVALID,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        error = NULL;
    }
    return request_name_result;
}

//获取序列号
QString Libactivation::get_serial_number()
{
    int ret = 0;
    GError *error = NULL;
    gchar new_serial[20]="0";
    guint request_name_int =0;
    gchar *request_name_str = NULL;
    if (proxy != NULL) {


        ret = dbus_g_proxy_call(proxy, "serial_number", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        error = NULL;
//        printf("serial_number->%s\n", request_name_str);
//        printf("serial_number_i->%d\n", request_name_int);

    }

    if(strlen(request_name_str) == 7)
        strcat(new_serial, request_name_str);
    else
        strcpy(new_serial, request_name_str);

    return new_serial;
}


static Libactivation _activation = Libactivation();

char* activation_get_harddisk_id()
{
    QString hd_id = _activation.get_harddisk_id();
    if (!hd_id.isNull() && !hd_id.isEmpty()) {
        return strdup(hd_id.toStdString().c_str());
    }

    return NULL;
}

char* activation_get_system_uuid()
{
    QString hd_id = _activation.get_system_uuid();
    if (!hd_id.isNull() && !hd_id.isEmpty()) {
        return strdup(hd_id.toStdString().c_str());
    }

    return NULL;
}

char* activation_get_interface_mac()
{
    QString hd_id = _activation.get_interface_mac();
    if (!hd_id.isNull() && !hd_id.isEmpty()) {
        return strdup(hd_id.toStdString().c_str());
    }

    return NULL;
}

char* activation_new_register_number(char *serial)
{
    QString hd_id = _activation.new_register_number(serial);
    if (!hd_id.isNull() && !hd_id.isEmpty()) {
        return strdup(hd_id.toStdString().c_str());
    }

    return NULL;
}

int activation_status_code()
{
    int hd_id = _activation.status_code();
    if (hd_id)
        return hd_id;

    return 0;
}

char* activation_get_serial_number()
{
    QString hd_id = _activation.get_serial_number();
    if (!hd_id.isNull() && !hd_id.isEmpty()) {
        return strdup(hd_id.toStdString().c_str());
    }

    return NULL;
}
